package com.lisheny.vip_movie;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.NavigationView;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.GravityCompat;
import android.support.v4.view.ViewCompat;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;

import com.lisheny.vip_movie.adapter.ContentPagetAdapter;
import com.lisheny.vip_movie.base.BaseActivity;
import com.lisheny.vip_movie.modules.movie.GoodFragment;
import com.lisheny.vip_movie.modules.movie.HotFragment;
import com.lisheny.vip_movie.modules.movie.NewFragment;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class MainActivity extends BaseActivity implements NavigationView.OnNavigationItemSelectedListener {

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.tabLayout)
    TabLayout tabLayout;
    @BindView(R.id.viewpager)
    ViewPager viewpager;
    @BindView(R.id.nav_view)
    NavigationView navView;
    @BindView(R.id.drawer_layout)
    DrawerLayout drawerLayout;
    @BindView(R.id.fab)
    FloatingActionButton fab;

    private List<String> tabIndicators;
    private List<Fragment> tabFragments;
    private ContentPagetAdapter contentPagetAdapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected int initLayout() {
        return R.layout.activity_main;
    }

    @Override
    protected void initView() {
        setSupportActionBar(toolbar);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawerLayout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawerLayout.setDrawerListener(toggle);
        toggle.syncState();

        navView.setNavigationItemSelectedListener(this);

        initTab();
        initContent();
    }

    @Override
    protected void initData() {
        tabIndicators = new ArrayList<>();
        tabIndicators.add("最近热映");
        tabIndicators.add("最新上映");
        tabIndicators.add("最受好评");

        tabFragments = new ArrayList<>();
        tabFragments.add(new HotFragment());
        tabFragments.add(new NewFragment());
        tabFragments.add(new GoodFragment());
    }


    @Override
    public void onBackPressed() {
        if (drawerLayout.isDrawerOpen(GravityCompat.START)) {
            drawerLayout.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        int id = item.getItemId();

        if (id == R.id.nav_movie) {

        } else if (id == R.id.nav_gallery) {

        } else if (id == R.id.nav_slideshow) {

        } else if (id == R.id.nav_manage) {

        } else if (id == R.id.nav_share) {

        } else if (id == R.id.nav_send) {

        }

        drawerLayout.closeDrawer(GravityCompat.START);
        return true;
    }

    private void initTab() {
        tabLayout.setTabMode(TabLayout.MODE_FIXED);
        tabLayout.setTabTextColors(ContextCompat.getColor(this, R.color.colorAccent), ContextCompat.getColor(this, R.color.white));
        tabLayout.setSelectedTabIndicatorColor(ContextCompat.getColor(this, R.color.white));
        ViewCompat.setElevation(tabLayout, 10);
        tabLayout.setupWithViewPager(viewpager);
    }

    private void initContent() {
        contentPagetAdapter = new ContentPagetAdapter(getSupportFragmentManager(), tabFragments, tabIndicators);
        viewpager.setAdapter(contentPagetAdapter);
    }

    @OnClick(R.id.fab)
    public void onViewClicked() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Document document = Jsoup.connect("http://www.xiuaiquan.top/app/index.php?i=2&c=entry&op=dianying&do=list&m=cyl_video_pc").get();
                    String str = document.title();
                    Elements elements = document.select("div.txtPadding");
                    for (Element e : elements) {

                        Log.d("抓取数据","标题："+e.select("span.sTit").select("a[href]").attr("title"));
                        Log.d("抓取数据","演员："+e.select("span.sActor").select("em"));
                        Log.d("抓取数据","播放地址："+e.select("span.sTit").select("a[href]").attr("href"));
                    }

                    Elements elementsv2 = document.select("div.pic");
                    for (Element e : elementsv2) {
                        Log.d("抓取数据","图片地址："+e.select("img[src$=.jpg]"));
                        Log.d("抓取数据","年代："+e.select("span.pRightBottom").select("em"));
                        Log.d("抓取数据","--------------------------------------------------");
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();

    }
}
